package com.yk.faceapplication;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatImageView;
import android.view.View;
import android.widget.ImageView;
import com.allinpay.xed.R;
import com.sensetime.stlivenesslibrary.util.Constants;


public class MainActivity extends AppCompatActivity {
    AppCompatImageView mImage;

    private final int FACE_REQUEST = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.m_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openLivenessActivity(MainActivity.this);
//                startActivity(new Intent(MainActivity.this,com.sensetime.stlivenesslibrary.ui.LivenessActivity.class));
            }
        });
    }


    public void openLivenessActivity(Activity activity) {
        try {
            Bundle bundle = new Bundle();
            StringBuilder sBuilder = new StringBuilder();
            sBuilder.append(Constants.BLINK + " ");
//            sBuilder.append(Constants.MOUTH + " ");
//            sBuilder.append(Constants.NOD + " ");
//            sBuilder.append(Constants.YAW + " ");
            /**
             * OUTPUT_TYPE 配置, 传入的outputType类型为singleImg （单图），multiImg （多图），video（低质量视频），fullvideo（高质量视频）
             */
            bundle.putString(com.sensetime.stlivenesslibrary.ui.LivenessActivity.OUTTYPE, Constants.MULTIIMG);
            /**
             * EXTRA_MOTION_SEQUENCE 动作检测序列配置，支持四种检测动作， BLINK(眨眼), MOUTH（张嘴）, NOD（点头）, YAW（摇头）, 各个动作以空格隔开。 推荐第一个动作为BLINK。
             * 默认配置为"BLINK MOUTH NOD YAW"
             */
            bundle.putString(com.sensetime.stlivenesslibrary.ui.LivenessActivity.EXTRA_MOTION_SEQUENCE, sBuilder.toString());
            /**
             * SOUND_NOTICE 配置, 传入的soundNotice为boolean值，true为打开, false为关闭。
             */
            bundle.putBoolean(com.sensetime.stlivenesslibrary.ui.LivenessActivity.SOUND_NOTICE, true);
            /**
             * COMPLEXITY 配置, 传入的complexity类型为normal,支持四种难度，easy, normal, hard, hell.
             */
            bundle.putString(com.sensetime.stlivenesslibrary.ui.LivenessActivity.COMPLEXITY, Constants.EASY);

            Intent intent = new Intent();
            intent.putExtras(bundle);
            intent.setClass(activity, com.sensetime.stlivenesslibrary.ui.LivenessActivity.class);
            intent.putExtras(bundle);
            activity.startActivityForResult(intent, FACE_REQUEST);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == FACE_REQUEST) {
            if(resultCode == RESULT_OK){
                byte[] img = data.getByteArrayExtra("image0");
                Bitmap bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);
                mImage.setImageBitmap(bitmap);
            }
        }
    }

}
